package com.liyunc.demo.service;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.liyunc.demo.model.dto.ProjectInfoDto;
import com.liyunc.demo.model.entity.ProjectInfo;
import com.liyunc.demo.service.base.BaseService;
import com.liyunc.demo.utils.ToDtoUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.security.RolesAllowed;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-02-17
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-17               liyuncong        Created
 */
@Service
public class ProjectService extends BaseService {

    /**
     * 获取所有项目.
     */
    @RolesAllowed({"ROLE_ADMIN"})
    public List<ProjectInfoDto> getProjectList(final int startIndex, final int pageSize) {
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(startIndex, pageSize, sort);
        return projectInfoRepository
            .findAll(pageable)
            .stream()
            .map(ToDtoUtils::forInfoEntity)
            .collect(Collectors.toList());
    }

    /**
     * 通过id获取单个项目.
     */
    public Optional<ProjectInfoDto> getSingleProject(long projectId) {
        if (authProject(projectId)) {
            return projectInfoRepository
                .findById(projectId).map(ToDtoUtils::forInfoEntity);
        }
        return Optional.empty();
    }

    /**
     * 创建项目.
     */
    @RolesAllowed({"ROLE_ADMIN"})
    public Optional<ProjectInfoDto> createProject(ProjectInfoDto infoDto) {
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setProjectName(infoDto.getProjectName());
        projectInfo.setProjectDescribe(infoDto.getProjectDescribe());
        projectInfo.setCreateTime(infoDto.getCreateTime());
        projectInfo.setLastTime(infoDto.getLastTime());
        ProjectInfo createInfo = projectInfoRepository.saveAndFlush(projectInfo);
        return Optional.of(createInfo).map(ToDtoUtils::forInfoEntity);
    }

    /**
     * 编辑项目.
     */
    @RolesAllowed({"ROLE_ADMIN"})
    public Optional<ProjectInfoDto> updateProject(ProjectInfoDto infoDto) {
        ProjectInfo projectInfo = projectInfoRepository.getById(infoDto.getId());
        projectInfo.setProjectName(infoDto.getProjectName());
        projectInfo.setProjectDescribe(infoDto.getProjectDescribe());
        projectInfo.setLastTime(LocalDateTime.now());
        ProjectInfo createInfo = projectInfoRepository.saveAndFlush(projectInfo);
        return Optional.of(createInfo).map(ToDtoUtils::forInfoEntity);
    }

    /**
     * 删除项目.
     */
    @RolesAllowed({"ROLE_ADMIN"})
    public void deleteProject(long id) {
        projectInfoRepository.deleteById(id);
    }
}
